package com.edu.publisher.controller;

import com.edu.publisher.bean.*;
import com.edu.publisher.service.TrafficStatsService;
import com.edu.publisher.util.DateFormatUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: TrafficStatsController
 * @Author: wqz
 * @Data: 2022/10/21 0:03
 * @Description: 流量域统计Controller
 */
@RestController
public class TrafficStatsController {

    @Autowired
    private TrafficStatsService trafficStatsService;

    // 1. 独立访客请求拦截方法
    @RequestMapping("/ch/uv")
    public String getChUvCt(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUvCt> trafficUvCtList = trafficStatsService.getChUvCt(date, limit);
        List chList = new ArrayList();
        List uvList = new ArrayList();
        for (TrafficUvCt trafficUvCt : trafficUvCtList) {
            chList.add(trafficUvCt.getCh());
            uvList.add(trafficUvCt.getUvCt());
        }

        String json = "{\"status\": 0,\"data\": {\"categories\": " +
                "[\"" + StringUtils.join(chList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"独立访客数\",\"data\": " +
                "[" + StringUtils.join(uvList, ",") + "]}]}}";
        return json;
    }

    // 2. 会话数请求拦截方法
    @RequestMapping("/ch/sv")
    public String getChSvCt(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficSvCt> trafficUvCtList = trafficStatsService.getChSvCt(date, limit);
        List chList = new ArrayList();
        List uvList = new ArrayList();
        for (TrafficSvCt trafficSvCt : trafficUvCtList) {
            chList.add(trafficSvCt.getCh());
            uvList.add(trafficSvCt.getSvCt());
        }

        String json = "{\"status\": 0,\"data\": {\"categories\": " +
                "[\"" + StringUtils.join(chList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"会话数\",\"data\": " +
                "[" + StringUtils.join(uvList, ",") + "]}]}}";
        return json;
    }

    // 3. 各会话浏览页面数请求拦截方法
    @RequestMapping("ch/pps")
    public String getChPvPerCt(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficPvPerSession> trafficPvPerSessionList = trafficStatsService.getChPvPerCt(date, limit);
        List chList = new ArrayList();
        List pvPerList = new ArrayList();
        for (TrafficPvPerSession trafficPvPerSession : trafficPvPerSessionList) {
            chList.add(trafficPvPerSession.getCh());
            pvPerList.add(trafficPvPerSession.getPvPerSession());
        }

        String json = "{\"status\": 0,\"data\": {\"categories\": " +
                "[\"" + StringUtils.join(chList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"会话平均页面浏览数\",\"data\": " +
                "[" + StringUtils.join(pvPerList, ",") + "]}]}}";
        return json;
    }

    // 4. 各会话累计访问时长请求拦截方法
    @RequestMapping("ch/dps")
    public String getChDurPerSession(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        {
            if (date == 0) {
                date = DateFormatUtil.now();
            }
        }
        List<TrafficDurPerSession> trafficDurPerSessionList = trafficStatsService.getChDurPerSession(date, limit);

        List chList = new ArrayList();
        List durPerList = new ArrayList();
        for (TrafficDurPerSession durPerSession : trafficDurPerSessionList) {
            chList.add(durPerSession.getCh());
            durPerList.add(durPerSession.getDurPerSession());
        }

        String json = "{\"status\": 0,\"data\": {\"categories\": " +
                "[\"" + StringUtils.join(chList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"会话平均页面访问时长\",\"data\": " +
                "[" + StringUtils.join(durPerList, ",") + "]}]}}";
        return json;
    }

    // 5. 跳出率请求拦截方法
    @RequestMapping("/ch/ujr")
    public String getChUjRate(
            @RequestParam(value = "date", defaultValue = "0") Integer date,
            @RequestParam(value = "limit", defaultValue = "10") Integer limit) {

        if (date == 0) {
            date = DateFormatUtil.now();
        }
        List<TrafficUjRate> chUjRateList = trafficStatsService.getChUjRate(date, limit);
        List chList = new ArrayList();
        List ujRateList = new ArrayList();
        for (TrafficUjRate trafficUjRate : chUjRateList) {
            chList.add(trafficUjRate.getCh());
            ujRateList.add(trafficUjRate.getUjRate());
        }

        String json = "{\"status\": 0,\"data\": {\"categories\": " +
                "[\"" + StringUtils.join(chList, "\",\"") + "\"]," +
                "\"series\": [{\"name\": \"跳出率\",\"data\": " +
                "[" + StringUtils.join(ujRateList, ",") + "]}]}}";
        return json;
    }

    // 6. 访客状态分时统计请求拦截方法
    @RequestMapping("/vph")
    public String getVisitorPerHr(
            @RequestParam(value = "date", defaultValue = "0") Integer date
    ) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }

        List<TrafficVisitorStatsPerHour> visitorPerHrList = trafficStatsService.getVisitorPerHr(date);

        if (visitorPerHrList == null || visitorPerHrList.size() == 0) {
            return "";
        }

        TrafficVisitorStatsPerHour[] perHrArr = new TrafficVisitorStatsPerHour[24];
        for (TrafficVisitorStatsPerHour trafficVisitorStatsPerHour : visitorPerHrList) {
            Integer hr = trafficVisitorStatsPerHour.getHr();
            perHrArr[hr] = trafficVisitorStatsPerHour;
        }

        String[] hrs = new String[24];
        Long[] uvArr = new Long[24];
        Long[] pvArr = new Long[24];
        Double[] pvPerSArr = new Double[24];

        for (int hr = 0; hr < 24; hr++) {
            hrs[hr] = String.format("%02d", hr);
            TrafficVisitorStatsPerHour trafficVisitorStatsPerHour = perHrArr[hr];
            if (trafficVisitorStatsPerHour != null) {
                uvArr[hr] = trafficVisitorStatsPerHour.getUvCt();
                pvArr[hr] = trafficVisitorStatsPerHour.getPvCt();
                pvPerSArr[hr] = trafficVisitorStatsPerHour.getPvPerSession();
            } else {
                uvArr[hr] = 0L;
                pvArr[hr] = 0L;
                pvPerSArr[hr] = 0D;
            }
        }

        String json = "{\n" +
                "  \"status\": 0,\n" +
                "  \"msg\": \"\",\n" +
                "  \"data\": {\n" +
                "    \"categories\": [\n\"" +
                StringUtils.join(hrs, "\",\"") + "\"\n" +
                "    ],\n" +
                "    \"series\": [\n" +
                "      {\n" +
                "        \"name\": \"独立访客数\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(uvArr, ",") + "\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"页面浏览数\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(pvArr, ",") + "\n" +
                "        ]\n" +
                "      },\n" +
                "      {\n" +
                "        \"name\": \"会话平均浏览页面数\",\n" +
                "        \"data\": [\n" +
                StringUtils.join(pvPerSArr, ",") + "\n" +
                "        ]\n" +
                "      }\n" +
                "    ]\n" +
                "  }\n" +
                "}";

        return json;
    }
}
